package com.softwarerusher.daoImpl_cym;

import com.softwarerusher.model.TJiacun;
import com.softwarerusher.model.TJiacunId;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * TJiacun entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.softwarerusher.model.TJiacun
 * @author MyEclipse Persistence Tools
 */

public class TJiacunDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TJiacunDAO.class);
	// property constants
	public static final String JIACUN_SUM = "jiacunSum";

	public void save(TJiacun transientInstance) {
		Transaction tran=getSession().beginTransaction();
		log.debug("updating TStaff instance");
		try {
			getSession().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
        tran.commit();
        getSession().flush(); 
       getSession().close();

	}

	public void delete(TJiacun persistentInstance) {
		Transaction tran=getSession().beginTransaction();
		log.debug("saving TStaff instance");
		try {
			getSession().save(persistentInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		 tran.commit();
	        getSession().flush(); 
	       getSession().close();
	}

	public TJiacun findById(com.softwarerusher.model.TJiacunId id) {
		log.debug("getting TJiacun instance with id: " + id);
		try {
			TJiacun instance = (TJiacun) getSession().get(
					"com.softwarerusher.model.TJiacun", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TJiacun instance) {
		log.debug("finding TJiacun instance by example");
		try {
			List results = getSession().createCriteria(
					"com.softwarerusher.model.TJiacun").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TJiacun instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TJiacun as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByJiacunSum(Object jiacunSum) {
		return findByProperty(JIACUN_SUM, jiacunSum);
	}

	public List findAll() {
		log.debug("finding all TJiacun instances");
		try {
			String queryString = "from TJiacun";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TJiacun merge(TJiacun detachedInstance) {
		log.debug("merging TJiacun instance");
		try {
			TJiacun result = (TJiacun) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TJiacun instance) {
		log.debug("attaching dirty TJiacun instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TJiacun instance) {
		log.debug("attaching clean TJiacun instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}